$(function () {
    let $editBtn = $("#btn-pub-news");
    $editBtn.click(function(){
        //判断组名是否为空
        let sName = $('#news-title').val();
        // alert(sName)
        if(!sName){
            fAlert.alertErrorToast("组名不能为空！！")
            return
        }
        // 判断权限
        let sGroupPer = $('#group-permissions').val();
        if (!sGroupPer || sName===[]){
            fAlert.alertErrorToast("请选择你的权限");
            return
        }
        let groId = $(this).data('news-id');
        console.log(groId);

        let url = groId? "/x-admin/group/" + groId + "/" : "/x-admin/group/add/";

        let method = groId? "PUT": "POST";
        
        let data = {
            'name':sName,
            'group_permission':sGroupPer
        };
        
        $.ajax({
            url: url,
            type:method,
            data:JSON.stringify(data),
            contentType:'application/json; charset=utf-8',
            dataType:'json',
        })
            .done(function (res) {
                if(res.errno==="0" && groId){
                    fAlert.alertSuccessToast("用户组修改成功，即将跳转到管理页面");
                    setTimeout(()=>{
                        window.location.href = "/x-admin/group/";
                    }, 1000)
                }
                else if(res.errno==="0" && !groId){
                    fAlert.alertSuccessToast("用户组创建成功，即将跳转到管理页面");
                    setTimeout(()=>{
                        window.location.href = "/x-admin/group/";
                    }, 1000)
                }
                else{
                    fAlert.alertErrorToast(res.errmsg);
                }
            })
            .fail((err)=>{
                fAlert.alertErrorToast("网络故障，请刷新重试");
            });


    });
})